package com.wdcg.fragment;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wdcg.R;
import com.wdcg.model.access.BonusAccess;
import com.wdcg.utils.LocationProcessor;
import com.wdcg.utils.LocationProcessor.LocationProcessorUpdate;
import com.wdcg.view.RadarView;

public class RadarFragment extends Fragment implements SensorEventListener, LocationProcessorUpdate {

	private SensorManager sensorManager;
	private Sensor accelerometer;
	private Sensor magnetometer;
	private float[] gravity;
	private float[] geomagnetic;
	
	private RadarView radarView;
	private LocationProcessor locationProcessor;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {                
		super.onCreate(savedInstanceState);
		
		//orientation sensor
		sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
		accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

		//location
    	locationProcessor = new LocationProcessor(getActivity(), this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.fragment_radar, container, false);
				
		return v;
	}
	
	@Override
	public void onPause() {
		super.onPause();
		sensorManager.unregisterListener(this);
		radarView.stopTimer();
		locationProcessor.disconnectLocationClient();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
		sensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
		radarView = (RadarView) getActivity().findViewById(R.id.radarView);
		radarView.setRadarPoints(BonusAccess.getAvailableBonusesDesctiption());
		radarView.startTimer();
		locationProcessor.connectLocationClient();
	}

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		
	}

	public void onSensorChanged(SensorEvent event) {
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
			gravity = event.values;
		if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
			geomagnetic = event.values;
		if (gravity != null && geomagnetic != null) {
			float R[] = new float[9];
			float I[] = new float[9];
			boolean success = SensorManager.getRotationMatrix(R, I, gravity, geomagnetic);
			if (success) {
				float orientation[] = new float[3];
				SensorManager.getOrientation(R, orientation);
				float azimuth = orientation[0];
				radarView.setRadarRotation((float)Math.toDegrees(-azimuth));
				radarView.invalidate();
			}
		}
	}

	public void onLocationChanged(Location location) {
		radarView.setCurrentLocation(location);
	}

}
